/****************************************************************
 * I am the AC 130 vs Pirate game. I allow the user to control an 
 * AC130 gunship in the hunt for Modern day pirates. The player  
 * will be allowed to roam free across a section of ocean/coastline.  
 * In the water there will be four types of vessels: Merchant  
 * ships that the player must protect, Pirate ships that the player  
 * must identify and destroy, Fishing boats that look like Pirate  
 * ships that the player must not destroy, and Friendly Navy ships  
 * that can capture pirates for extra points. Time permitting there  
 * may also be ground targets to attack. 
 *
 * Key elements of this project will be the Player/AC130, the Ships,  
 * the ShipAI system, the collision grid, the debris controller, the  
 * Ground/Water, Clouds, and scoring system. This project will make  
 * use of an external text file to store useful game parameters,  
 * thus allowing us to play balance the game without recompiling  
 * the executable. 
 *****************************************************************
 * This is the Main.cpp File where the program's main loop occures.
 * Most of the work is done in the World class. However Allegro 
 * initilzation happens here
 *****************************************************************/

#include "ScreenCtrl.h"

#include <allegro.h>
#include <direct.h>
#include <stdio.h>

// Globals
bool updateFlag;

/******************************************************************
 * I am timer call back function
 *****************************************************************/
void UpdateFunc(){
	updateFlag = true;
	return;
}

/******************************************************************
 * I am the program starting point and guardian of the game loop
 *****************************************************************/
int main(int argc, char *argv[])
{
	bool notDone = true;
	// test read
//	char loc[200]; 
//	_getcwd(loc, 200);
//	char * fname = "C:\\Code\\AC130vPirates\\GameData.txt";
//	FILE * data = fopen(fname, "r");
		////////////////////////////////////////////
		//			Initilization Section
		////////////////////////////////////////////
	allegro_init();
	install_keyboard(); 
	//install_mouse();
	//install_joystick(JOY_TYPE_AUTODETECT);;

	set_color_depth(16);

		// initilize window
	//GFX_AUTODETECT_FULLSCREEN
	//GFX_AUTODETECT_WINDOWED
		// try loading in by tiers
	if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 1024, 768, 0, 0) != 0) {
		if (set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 1024, 768, 0, 0) != 0) {
			if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0) != 0) {
				// if you are here graphic initilization failed
				set_gfx_mode(GFX_TEXT, 0,0,0,0);
				allegro_message("Graphics Initilization Failed");
				return 1;
			}
		}
	}
		// initilize world
	updateFlag = false;
	rest(10);	// wait for world to finish initilization
				// before starting timer


		// initilize timer
	if ( install_int(UpdateFunc, 60) != 0){
			// if you are here timer initilization failed
		set_gfx_mode(GFX_TEXT, 0,0,0,0);
		allegro_message("Timer Initilization Failed");
		return 1;
	}
		// intilize Mouse
	if ( install_mouse() == -1){
			// if you are here timer initilization failed
		set_gfx_mode(GFX_TEXT, 0,0,0,0);
		allegro_message("Mouse Initilization Failed");
		return 1;
	}
		// initilize Joysticks
	if (install_joystick(JOY_TYPE_AUTODETECT) != 0){
		allegro_message("Joystick Initilization Failed");
	}
		// initilize sound
	if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0) {
      allegro_message("Sound Initilization Failed");
      return 1;
    }
	
		// initilize screen controll
	ScreenCtrl * myScreenCtrl = new ScreenCtrl;	// pointer to screen controll object


		////////////////////////////////////////////
		//			Game Loop Sections
		////////////////////////////////////////////
	while ( notDone )
	{
			// exit conditions
		if(key[KEY_ESC] || 
			myScreenCtrl->gd->nxtScreen == stQuit) notDone = false;
			// update movement and screen on true update flag
		if(updateFlag == true){
			updateFlag = false;
			myScreenCtrl->Update();
			myScreenCtrl->Draw();
			//blit( myWorld->cam->screen, screen, 0,0, 0,0, SCREEN_W, SCREEN_H);
				// stretch screen to window size
	
				// check exit condition
			//if (myWorld->myOverlay->mode < 0 ) notDone = false;	// we are now done || key[KEY_ESC]
		}
		rest(1);	// to be freindly to labtop power consumption
	}	
		////////////////////////////////////////////
		//			Clean Up Time
		////////////////////////////////////////////
	allegro_exit(); 
	//delete(myScreenCtrl);

	return 0;
}
END_OF_MAIN();